var jsonFile = require('jsonfile')
var fileName = 'cy.json'
const axios = require('axios')
var jsonData = jsonFile.readFileSync(fileName)
const qs = require('qs')

const phone = jsonData.phone
const pwd = jsonData.pwd
const token = jsonData.token
const pay_pwd = jsonData.pay_pwd
const goods = jsonData.goods
var requestArray = []

const instance = axios.create({
  credentials: true,
  baseURL: 'https://api.cyxk.net',
  headers: { 'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1' }
})
instance.defaults.headers['Content-Type'] = 'application/json;'
instance.defaults.headers['authorization'] = token





pushList()


// 商品列表填充
function pushList() {
  setInterval(() => {
    for (let item in goods) {
      instance.post('/api.html', { method: 'market.getlist', goods_cat_id: [], order: 'give_price asc', page: 1, limit: 10, token }).then(res => {
        let data = res.data.data[0]
        let price = data.give_price
        let sn = data.sn
        let name = data.goods_name
        console.log('商品:' + name + '最低价为:' + price + '------价格低于:' + goods[item].price + '时将自动锁定')
        if (price <= goods[item].price) {
          instance.post('/api.html', { method: 'market.create', goods_id: sn, sn: sn, source: 2, token: token, pay_password: pay_pwd }).then(res => {
            if (res.data.status == true) {
              console.log('下单成功!')
              axios.post('https://open.feishu.cn/open-apis/bot/v2/hook/9234b14f-e535-41e2-98dd-309f1fd2b580', { "msg_type": "text", "content": { "text": "十艺锁单成功,请上号支付!!!" + ",价格为:" + price } })
            } else {
              console.log(res.data)
            }
          }).catch(err => {
            console.log(err)
          })
        }
      }).catch(err => {
        console.log(err)
      })
    }
  }, 100)
}